"""
@author: XDing
@file: 验证IP地址的合法性.py
@time: 2023/5/30 17:30
"""

# 验证ipv4地址合法性
#1~255.1~255.1~255.1~255

import re

def is_valid_ipv4(ip):
    pattern = re.compile(r'^(\d{1,3}\.){3}\d{1,3}$')
    if pattern.match(ip):
        octets = ip.split('.')
        if all(0 <= int(octet) <= 255 for octet in octets):
            return True
    return False

ip_address = input("请输入一个IPv4地址：")
if is_valid_ipv4(ip_address):
    print("输入的IPv4地址合法。")
else:
    print("输入的IPv4地址不合法。")